package com.zls.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zls.mapper.TDeviceGatherMapper;
import com.zls.mapper.TDeviceTypeMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.TDevice;
import com.zls.pojo.TDeviceGather;
import com.zls.pojo.TDeviceType;
import com.zls.service.TDeviceGatherService;
import com.zls.service.TDeviceService;
import com.zls.service.TDeviceTypeService;
import com.zls.service.TFarmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TDeviceGatherServiceImpl extends ServiceImpl<TDeviceGatherMapper, TDeviceGather> implements TDeviceGatherService {
    @Autowired
    private TDeviceGatherMapper tDeviceGatherMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<TDeviceGather> page = tDeviceGatherMapper.findPage((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = tDeviceGatherMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Autowired
    private TDeviceService tDeviceService;
    @Autowired
    private TDeviceGather tDeviceGather;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void addGather() {
        tDeviceService.findAll();
        //获取所有设备id集合
        List<Integer> ids = tDeviceService.findIds();
        //数据格式 分为三部分 头 体 尾
        //必须以7b55开头,7a55结尾
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("7b55");
        //body 包含信息 id 数据 如:1a22 1代表设备id ,a为分隔符 22代表数据值
        //动态生成
        Random random = new Random();
        //动态获取设备id
        Integer integer = ids.get(random.nextInt(ids.size()));
        //根据id查设备信息
        TDevice tDevice = tDeviceService.selectById(integer);
        //随机产生值
        String body=integer+"a"+random.nextInt(40);
        //生成最终数据
        String message=stringBuilder.append(body).append("7a55").toString();
        System.out.println("定时器触发每隔一小时"+new Date()+message);
        /**
         * 校验数据
         */
        if (message.length()<=13 && message.startsWith("7b55") && message.endsWith("7a55")){
            /**
             * 去头去尾
             */
            String message1 = StringUtils.removeStart(message, "7b55");
            String message2 = StringUtils.chomp(message1,"7a55");
            String message3 =StringUtils.remove(message2,integer+"a");
            /**
             * 判断数据是否在设备筏值内
             */
            //通过设备id获取redis的hash值
//            Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(String.valueOf(integer));
//            System.out.println(entries);
            String min = (String) stringRedisTemplate.opsForHash().get(String.valueOf(integer), "min");
            String max = (String) stringRedisTemplate.opsForHash().get(String.valueOf(integer), "max");
            if (Integer.parseInt(message3)>Float.valueOf(max) || Integer.parseInt(message3)<Float.valueOf(min)){
                System.err.println("警告!警告!采集数据不在设备阈值内,请及时处理!!!");
            }

            /**
             * 添加数据
             */
            if (tDevice!=null && tDevice.getSn()!=null){
                tDeviceGather.setDeviceSn(tDevice.getSn());
            }
            if (tDevice!=null && tDevice.getName()!=null){
                tDeviceGather.setDeviceName(tDevice.getName());
            }
            tDeviceGather.setDeviceType(tDevice.getTDeviceType().getName());
            tDeviceGather.setDeviceId(tDevice.getId());
            tDeviceGather.setCreateTime(new Date());
            tDeviceGather.setUpdateTime(new Date());
            tDeviceGather.setGatherTime(new Date());
            tDeviceGather.setBasicData(message3);
            int i = tDeviceGatherMapper.insertOne(tDeviceGather);
            if (i>0){
                System.out.println(tDevice.getName()+"此次数据:"+message3);
                System.out.println("成功插入一条");
            }else {
                System.out.println("插入失败");
            }

        }

    }
}
